using System;
using System.Collections.Generic;
using System.Xml;

namespace BIUtilities.Publish
{
    /// <summary>
    /// Class used to deploy files / items using sharepoint webservice apis
    /// </summary>
    public class SharepointDeployUtility
    {
        SharepointListsWebservice.Lists _targetWebservice = null;

        /// <summary>
        /// Set the target webservice for a sharpeoint site to update
        /// </summary>
        public string TargetSiteUrl
        {
            get { return _targetWebservice.Url; }
            set
            {
                _targetWebservice = new SharepointListsWebservice.Lists();
                _targetWebservice.Url = (value.TrimEnd('/') + "/_vti_bin/lists.asmx");
                _targetWebservice.UseDefaultCredentials = true;
            }
        }

        /// <summary>
        /// With a given list name, look up the document library id (guid)
        /// </summary>
        /// <param name="listName"></param>
        /// <returns></returns>
        public string GetDocumentLibraryId(string listName)
        {
            XmlNode listNode = _targetWebservice.GetList(listName);
            if (listNode != null)
            {
                XmlAttribute idAttribute = listNode.Attributes["ID"];
                return idAttribute.Value;
            }
            throw new Exception("List name(" + listName + ") is not found on target sharepoint server: " + _targetWebservice.Url);
        }
    }
}
